<?php

namespace app\controller;

use app\BaseController;
use app\model\UserLogs;
use app\model\Users;
use think\facade\View;
use think\Request;

class User extends BaseController
{
    static $users;          //用户表
    static $user_logs;      //用户log表

    public function __construct(array $data = [])
    {
        self::$users = new Users();
        self::$user_logs = new UserLogs();
    }

    // +---------------------------------------------------------
    // +-------------------     视   图    -----------------------

    //用户列表
    public function index(Request $request)
    {
        //每页显示多少条
        $sort = 20;
        //当前页
        $curr = $request->param('page')?$request->param('page'):1;
        //偏移量
        $offset = ($curr - 1) * $sort;
        //总条数
        $count = self::$users->count();
        $keyword = $request->param('key');
        if ($keyword){
            $where[]=['mobile','like', '%'.$keyword.'%'];
        }else{
            $where = [];
        }
        $limit[0] = $offset;
        $limit[1] = $sort;
        $user_list = self::$users->get_users($where,['create_time'=>'desc'],$offset,$sort);
        $user_list = $user_list ? $user_list->toArray() : [];
        View::assign('curr',$curr);
        View::assign('sort',$sort);
        View::assign('count',$count);
        View::assign('keyword',$keyword);
        View::assign('user_list',$user_list);
        return View::fetch();
    }

    //添加用户
    public function add(Request $request)
    {
        if (is_get()){
            //视图
            return View::fetch();
        }else{
            //执行添加操作
            //开启事务
            self::$users->startTrans();
            self::$user_logs->startTrans();
            try {

                $mobile = getv('mobile');
                $name = getv('name');
                $cost = getv('cost');
                //判断手机号是否是 11 位
                if (strlen($mobile) != 11){
                    returnJson(false,2001,'请输入11位正确的手机号~');
                }
                //判断手机号是否被占用
                $user = self::$users->get_info(['mobile'=>$mobile],'id');
                if (!!$user){
                    returnJson(false,2002,'手机号已被占用~');
                }
                $data = [];
                $data['mobile'] = $mobile;
                $data['name'] = $name;
                $data['total_cost'] = $cost?$cost:0;
                $data['create_time'] = time();
                $id = self::$users->create($data)->id;
                if ($cost == ''){
                    $log = [
                        ['uid'=>$id,'des'=>'会员开户','cost'=>0,'type'=>0,'time'=>time()],
                    ];
                }else{
                    $log = [
                        ['uid'=>$id,'des'=>'会员开户','cost'=>0,'type'=>0,'time'=>time()],
                        ['uid'=>$id,'des'=>'金额添加','cost'=>$cost,'type'=>1,'time'=>time()+1]
                    ];
                }
                self::$user_logs->insertAll($log);
                //提交事务
                self::$users->commit();
                self::$user_logs->commit();
                returnJson(true,0,'添加成功~');
            }catch (\Exception $e){
                //事务回滚
                self::$users->rollback();
                self::$user_logs->rollback();
            }
        }
    }

    //用户修改
    public function update(Request $request)
    {
        //开启事务
        self::$users->startTrans();
        self::$user_logs->startTrans();
        try {
            $id = getv('id');
            $name = getv('name');
            param_empty([$id,$name]);
            $user_info = self::$users->get_info(['id'=>$id],'id,name');
            if (!$user_info){
                returnJson(false,2,'用户不存在！！');
            }
            $data['id'] = $id;
            $data['name'] = $name;
            self::$users->update($data);
            $log['uid'] = $id;
            $log['type'] = 0;
            $log['time'] = time();
            $log['des'] = '修改用户名，'.$user_info->name.'改为：'.$name;
            self::$user_logs->save($log);
            //提交事务
            self::$users->commit();
            self::$user_logs->commit();
            returnJson(true,0,'修改成功~');
        }catch (\Exception $e){
            //事务回滚
            self::$users->rollback();
            self::$user_logs->rollback();
        }
    }

    //会员查找
    public function show(Request $request)
    {
        $mobile = intval($request->param('m'));
        $error = '用户不存在！';
        if (strlen($mobile) != 11){
            $error = '手机号格式不正确';
        }
        $user_info = self::$users->get_info(['mobile'=>$mobile],'id,name,mobile,total_cost,create_time');
        if (!$user_info){
            $user_info =  [];
            $user_logs = [];
        }else{
            $user_info = $user_info->toArray();
            $user_logs = self::$user_logs->get_logs(['uid'=>$user_info['id']]);
            $error = '';
        }
        View::assign('error',$error);
        View::assign('mobile',$mobile);
        View::assign('user_info',$user_info);
        View::assign('user_logs',$user_logs);
        return View::fetch();
    }

}
